$def with (person, input={}, errors={})

$ _x = ctx.setdefault('cssfile', 'admin')
$ _x = ctx.setdefault('usergroup', 'admin')

$var title: $_("[Admin Center]") $person.displayname

<style type="text/css">

div.status {
    background-color: #eeeeee;
    border: 1px solid #cccccc;
    padding: 10px;

    -webkit-border-radius: 10px;
    -moz-border-radius: 10px;
    border-radius: 10px;

    margin-bottom: 20px;
}

div.status-active, div.status-verified {
    background: #dfd;
}

div.status-pending {
    background: #ffd;
}

span.invalid {
    padding-left: 20px;
    float: right;
}

span.status {
    font-weight: bold;
    padding: 4px;
    font-size: 1.1em;

    -webkit-border-radius: 4px;
    -moz-border-radius: 4px;
    border-radius: 4px;
}

span.safe {
    background-color: #aaffaa;
    padding: 0 3pt;
    color: black;
}

span.warning {
    /*background-color: #ff8888; */
    padding: 0 3pt;
    /*color: black;*/
    color: darkred;
}

form.account-actions {
    display:inline;
}

.borrow-table .who {
    display: none;
}

span.time {
    font-weight: bold;
}

input.big {
    font-size: 1.0em !important;
    width: 20em !important;
}

a.tag {
	padding: 5px;
	background: #eee;
	border: 1px solid #ddd;
	border-radius: 5px;
	cursor: pointer;
	text-decoration: none;
	color: #aaa;
    font-size: 1.1em;
}

a.tag:hover {
	text-decoration: underline;
	color: black;
}

a.tag.active {
	background: #fffdcd;
	border-color: #ccb;
    color: black;
}
</style>

<div id="contentHead">
    $:render_template("admin/menu")
    <h1><a class="plain" href="/admin/people">$_("People")</a> / $person.username</h1>
</div>

$ has_profile = person.get_user() is not None


<div id="contentBody">

    <div class="status status-$person.status">
        <form method="POST">
            Status: <span class="status">$person.status</span>

        $if person.status in ['active', 'verified']:
            <button class="do-confirm" type="submit" name="action" value="block_account_and_revert" style="float: right; background: #ffbbbb;">$_("block and revert all edits")</button>
            <button class="do-confirm" type="submit" name="action" value="block_account">$_("block this account")</button>
            <br/>
            <br/>
            <div class="small">$:_('Registered on <span class="time">%(date)s</span>.', date=datestr(person.registered_on, relative=False))</div>
            $ v = person.get_creation_info()
            <button type="submit" name="action" value="su" style="float: right; background: #ffbbbb;">$_("login as this user")</button>
            <div class="small">$:_('Activated on <span class="time">%(date)s</span> from <a href="/admin/ip/%(ip)s">%(ip)s</a>.', date=datestr(person.activated_on, relative=False), ip=v.ip)</div>
        $if person.status == "blocked":
            <button type="submit" name="action" value="unblock_account">$_("unblock this account")</button>
        $elif person.status == "pending":
            <button type="submit" name="action" value="activate_account">$_("activate account")</button>
            <br/>
            <br/>
            <div class="small">$:_('Registered on <span class="time">%(date)s</span>.', date=datestr(person.registered_on, relative=False))></div>
            <div class="small">
                $if person.get_activation_link():
                    $:_('Activation link expiring on <span class="time">%(date)s.</span>', date=datestr(person.get_activation_link().get_expiration_time()))
                $else:
                    <strong>$_("Activation link expired").</strong>

                <button type="submit" name="action" value="resend_link">$_("Resend activation link")</button>.
            </div>
        </form>
    </div>

  <table id="adminHistory" style="clear: both;">
    <tbody>
    <tr>
        <td>$_("Name:")</td>
        <td>$person.displayname - <a href="/people/$person.username" style="color: #900; text-decoration: none;">$_("view profile")</a></td>
    </tr>
    <tr>
        <td>$_("Email Address:")</td>
        <td>
            <form method="POST" name="form-email" id="form-email">
                <input type="email" name="email" id="email" class="big" value="$input.get('email', person['email'])"/>
                <button type="submit" name="action" value="update_email">$_("Change")</button>
                <span class="invalid" htmlfor="email" generated="true">$errors.get("email")</span>
            </form>
        </td>
    </tr>
    <tr>
        <td>$_("Reset Password")</td>
        <td>
            <form method="POST" name="form-password" id="form-password">
                <input type="text" name="password" id="password" class="password big" value="$input.get('password')"/>
                <button type="submit" name="action" value="update_password">$_("Change")</button>
                <span class="invalid" htmlfor="password" generated="true">$errors.get("password")</span>
            </form>
        </td>
    </tr>
    $if person.get_user() and person.get_user().is_admin():
        <tr><td>$_("Admin")</td><td>$_("Yes")</td></tr>
    <tr>
        <td>$_("Bot")</td>
        <td>
            $if has_profile:
                <form method="POST" name="form-bot" id="form-bot">
                    <input type="hidden" name="action" value="set_bot_flag">
                    <input type="hidden" name="bot" value="$(not person.get('bot', False))">
                    $if person.get('bot'):
                        $_("Yes")
                        <button type="submit">$_("Make Human")</button>
                    $else:
                        $_("No")
                        <button type="submit">$_("Make Bot")</button>
                </form>
            $else:
                <em>$_("Not available")</em>
        </td>
    </tr>
    <tr>
        <td>$_("# Edits")</td>
        <td>$person.get_edit_count()</td>
    </tr>
    <tr>
        <td>$_("Member Since:")</td>
        <td>
            $if person.activated_on:
                $datestr(person.activated_on)
            $else:
                <em>$_("Not available")</em>
        </td>
    </tr>
    <tr>
        <td>$_("Last Login:")</td>
        <td>
            $if person.last_login:
                $datestr(person.last_login)
            $else:
                <em>$_("Not available")</em>
        </td>
    </tr>
    <tr>
        <td>$_("Tags:")</td>
        <td>
            $ tags = sorted(["beta-tester", "trusted-user", "staff", "alumnus"])
            $for t in tags:
                <a class="tag $cond(person.has_tag(t), 'active')">$t</a>
        </td>
    </tr>
    <tr>
        <td>$_("Anonymize Account:")</td>
        <td>
            <form method="POST" id="anonymize-form">
                <input type="checkbox" id="dry-run-checkbox" name="dry_run">
                <label for="dry-run-checkbox">$_("Dry Run")</label>
                <button type="submit" name="action" value="anonymize_account" style="float: right;" class="account-anonymization-button" data-display-name="$person.username">$_("Anonymize Account")</button>
            </form>
        </td>
    </tr>
    </tbody>
  </table>

<h2>$_("Loans")</h2>
<div>
$ user = person.get_user()
$if user:
  $:render_template("admin/loans_table.html", user.get_loans())
$else:
  <em>$_("Account not activated yet.")</em>
</div>

$if user:
    <h2>$_("Waiting Loans")</h2>
    $:render_template("admin/loans_table.html", user.get_waitinglist(), waiting_loan=True)

<h2>
    $_("Edit History")
    <span class="link"><a href="/admin/people/$person.username/edits">$_('%(num)d edits', num=person.get_edit_count())</a></span>
</h2>
$if person['status'] == "verified" or person['status'] == "active":
    $:render_template("admin/history", person.get_user().get_edit_history())
$else:
    <em>$_("Account not activated yet.")</em>


<h2>$_("Debug Info")</h2>

<h3>$_("Account Information")</h3>
$# detect-missing-i18n-skip-line
<pre>$:json_encode(hasattr(person, "dict") and person.dict(), indent="    ", sort_keys=True)</pre>

<h3>$_("Verifications Links")</h3>
<div>
$for link in person.get_links():
        <strong>$link['_key']</strong>
        <br/>
        <pre>$:json_encode(link, indent="    ", sort_keys=True)</pre>
    </li>
</div>
$if not person.get_links():
    <em>$_("None found")</em>
</div>
